import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '../views/Home.vue'
import Login from '../views/Login.vue'
import {message} from 'ant-design-vue'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    name: 'Home',
    component: Home
  },
  {
    path:'/Login',
    name:'Login',
    component: Login
  }
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

router.beforeEach((to,from,next) => {
  //to将要访问的路径
  //from代表从那个路径跳转而来
  //next是一个函数，表示放行
  //next（）放行 next(’/login’)强制跳转
  if(to.path==='/login') return next()
  //获取token
  const tokenStr = window.sessionStorage.getItem('token')
  if(!tokenStr){
    message.error('身份验证失败！')
    return next('/login')
  }
    return next()
  })
export default router
